//
//  EmotionToolBar.m
//  Weibo
//
//  Created by Anasue on 9/25/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#define EmotionToolBarBtnCount 4

#import "EmotionToolBar.h"

@interface EmotionToolBar ()
@property (nonatomic, weak) UIButton *selectedBtn;
//@property (nonatomic, weak) UIButton *defaultBtn;


@end

@implementation EmotionToolBar

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //创建4个button
        [self addButtonWithTitle:@"最近" tag:EmotionTypeRecent];
        [self addButtonWithTitle:@"默认" tag:EmotionTypeDefault];
        [self addButtonWithTitle:@"emoji" tag:EmotionTypeEmoji];
        [self addButtonWithTitle:@"浪小花" tag:EmotionTypeLxh];
        
        //监听删除通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(emotionDidDelete:) name:EmotionDidDeleteNotification object:nil];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)emotionDidDelete:(NSNotification *)note
{
    if (self.selectedBtn.tag == EmotionTypeRecent) {
        [self buttonClick:self.selectedBtn];
    }
}

- (UIButton *)addButtonWithTitle:(NSString *)title tag:(EmotionType)tag
{
    UIButton *btn = [[UIButton alloc]init];
    btn.tag = tag;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    
    NSInteger count = self.subviews.count;
    if (count == 1) {
        [btn setBackgroundImage:[UIImage resizeImage:@"compose_emotion_table_left_normal"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage resizeImage:@"compose_emotion_table_left_selected"] forState:UIControlStateSelected];
    }else if (count == EmotionToolBarBtnCount){
        [btn setBackgroundImage:[UIImage resizeImage:@"compose_emotion_table_right_normal"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage resizeImage:@"compose_emotion_table_right_selected"] forState:UIControlStateSelected];
    }else{
        [btn setBackgroundImage:[UIImage resizeImage:@"compose_emotion_table_mid_normal"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage resizeImage:@"compose_emotion_table_mid_selected"] forState:UIControlStateSelected];
    }
    return btn;
}

- (void)buttonClick:(UIButton *)btn
{
        self.selectedBtn.selected = NO;
        btn.selected = YES;
        self.selectedBtn = btn;
    if ([self.delegate respondsToSelector:@selector(emotionToolBar:didClickBtn:)]) {
        [self.delegate emotionToolBar:self didClickBtn:btn.tag];
    }
}

- (void)setDelegate:(id<EmotionToolBarDelegate>)delegate
{
    _delegate = delegate;
    UIButton *btn = (UIButton *)[self viewWithTag:EmotionTypeDefault];
    [self buttonClick:btn];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat btnW = self.size.width / EmotionToolBarBtnCount;
    CGFloat btnH = self.size.height;
    for (int i = 0; i < EmotionToolBarBtnCount; i ++) {
        UIButton *btn = self.subviews[i];
        btn.width = btnW;
        btn.height = btnH;
        btn.x = i * btnW;
    }
}

@end
